package com.cgtong.android.base;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.cgtong.android.R;
import com.cgtong.android.util.StatusBarUtil;
import com.dyhdyh.widget.loading.dialog.LoadingDialog;
import com.dyhdyh.widget.loading.factory.MaterialDialogFactory;
import com.kingja.loadsir.callback.Callback;
import com.kingja.loadsir.core.LoadService;
import com.kingja.loadsir.core.LoadSir;
import com.umeng.analytics.MobclickAgent;

import butterknife.ButterKnife;

/**
 * Activity布局带有标题栏，同时初始化时，内容显示loading，需要继承此类
 * （如果初始化时，不需要显示loading,直接在loadDataByNet方法中，调用PostUtil.postCallbackDelayed(loadService, SuccessCallback.class)
 * 或者PostUtil.postSuccessDelayed(loadService)，和直接继承BaseActivity相同;）
 * by zhuqingchun
 */
public abstract class BaseTitleActivity extends AppCompatActivity {


    protected void backClick() {
        finish();
    }

    protected abstract String getContentTitle();

    protected abstract int getLayoutId();

    protected abstract void loadDataByNet();

    protected abstract void onDataReloadByNet(View v);

    protected View rootView;
    protected LoadService loadService;
//    public ImageView mBackLeftImg;
    public TextView mTitleLeftTxt;
    public TextView mTitleCenterTxt;
    public TextView mTitleRightTxt;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        rootView = View.inflate(this, R.layout.include_activity_title_loading_layout, null);
        addContent();
        setContentView(rootView);
        initStatusBar();
        loadDataByNet();
    }


    private void addContent() {
        FrameLayout flContent = (FrameLayout) rootView.findViewById(R.id.fl_content);
//        mBackLeftImg = (ImageView) rootView.findViewById(R.id.back_left_img);
        mTitleLeftTxt = (TextView) rootView.findViewById(R.id.title_left_txt);
        mTitleCenterTxt = (TextView) rootView.findViewById(R.id.title_center_txt);
        mTitleRightTxt = (TextView) rootView.findViewById(R.id.title_right_txt);

        mTitleCenterTxt.setText(getContentTitle() == null ? "" : getContentTitle());
        mTitleLeftTxt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                backClick();
            }
        });
        View content = View.inflate(this, getLayoutId(), null);
        if (content != null) {
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
                    FrameLayout.LayoutParams.MATCH_PARENT);
            flContent.addView(content, params);
            ButterKnife.bind(this, rootView);
            loadService = LoadSir.getDefault().register(content, new Callback.OnReloadListener() {
                @Override
                public void onReload(View v) {
                    onDataReloadByNet(v);
                }
            });
        }
    }


    /**
     * 子类可以重写改变状态栏颜色
     */
    protected int setStatusBarColor() {
        return R.color.colorPrimary;
    }

    /**
     * 子类可以重写决定是否使用透明状态栏
     */
    protected boolean setTranslucentStatusBar() {
        return false;
    }

    /**
     * 设置状态栏颜色
     */
    protected void initStatusBar() {
        if (setTranslucentStatusBar()) {
            // 设置状态栏全透明
            StatusBarUtil.transparencyBar(this);
            return;
        }
        StatusBarUtil.setStatusBarColor(this, setStatusBarColor());
    }

    /**
     * 显示加载框
     */
    public void doShowLoading(Context context, boolean cancelable) {
        this.doShowLoading(context, null, cancelable);
    }

    public void doShowLoading(Context context, String msg, boolean cancelable) {
        LoadingDialog loadingDialog = LoadingDialog.make(context, new MaterialDialogFactory());
        if (!TextUtils.isEmpty(msg)) {
            loadingDialog.setMessage(msg);//提示消息
        } else {
            loadingDialog.setMessage(getString(R.string.request_net))//提示消息
                    .setCancelable(cancelable);
        }
        loadingDialog.create().show();
    }

    public void doHideLoading() {
        LoadingDialog.cancel();
    }

    //友盟页面统计
    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(getContentTitle());
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(getContentTitle());
        MobclickAgent.onPause(this);
    }
}